<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="账户"
          label="账户"
          placeholder="账户"
          :rules="[{ required: true, message: '请填写账户' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
          v-model="checkPassword"
          type="password"
          name="确认密码"
          label="确认密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请再次确认密码' }]"
      />
      <van-field
          v-model="planetCode"
          name="专属ID"
          label="专属ID"
          placeholder="ID"
          :rules="[{ required: true, message: '请填写专属ID' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        注册
      </van-button>
    </div>
    </van-form>
</template>

<script setup lang="ts">
import {ref} from "vue";
import { useRoute,useRouter } from 'vue-router';
import myAxios from "../plugins/myAxios";
import {Toast} from "vant";

const router = useRouter();
const route = useRoute();
const userAccount = ref('');
const userPassword = ref('');
const checkPassword = ref('');
const planetCode = ref('');

const onSubmit = async () => {
  const res = await myAxios.post('/user/register', {
    userAccount: userAccount.value,
    userPassword: userPassword.value,
    checkPassword: checkPassword.value,
    planetCode : planetCode.value,
  })
  console.log(res, "用户注册");
  if (res.code === 0 && res.data) {
    Toast.success("注册成功");
    router.replace('/user/login');
    // const redirectUrl = route.query?.redirect as string ?? '/';
    // window.location.href = redirectUrl;
  } else {
    Toast.fail("注册失败！" + res.description);
  }


};
</script>

<style scoped>

</style>